package gui;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import org.xml.sax.SAXException;

import util.Constants.EVEData;
import ctrl.DBAccess;
import ctrl.EVECentralCtrl;
import data.EVEItem;

public class MainMenuBar extends JMenuBar implements ActionListener {

  private JMenu                _mainMenu;
  private JMenuItem            _addCollection;
  private JMenuItem            _addItem;
  private JMenuItem            _addSystem;
  private JMenuItem            _saveSelection;
  private JMenuItem            _updateMarketData;

  private CollectionTabbedPane _collectionPane;
  private EVECentralCtrl       _evecctrl;
  private DBAccess             _dataBase;

  public MainMenuBar(CollectionTabbedPane tabbedPane, EVECentralCtrl evecctrl, DBAccess dataBase) {
    super();

    _collectionPane = tabbedPane;
    _evecctrl = evecctrl;
    _dataBase = dataBase;

    _mainMenu = new JMenu("Main");
    _addCollection = new JMenuItem("add collection");
    _addItem = new JMenuItem("add item");
    _addSystem = new JMenuItem("add system");
    _saveSelection = new JMenuItem("save selection");
    _updateMarketData = new JMenuItem("update market data");

    add(_mainMenu);
    _mainMenu.add(_addCollection);
    _mainMenu.add(_addItem);
    _mainMenu.add(_addSystem);
    _mainMenu.addSeparator();
    _mainMenu.add(_saveSelection);
    _mainMenu.addSeparator();
    _mainMenu.add(_updateMarketData);
    _addCollection.addActionListener(this);
    _addItem.addActionListener(this);
    _addSystem.addActionListener(this);
    _saveSelection.addActionListener(this);
    _updateMarketData.addActionListener(this);
    
    _mainMenu.addMenuListener(new MenuListener() {
      @Override
      public void menuSelected(MenuEvent arg0) {
        if(_collectionPane.getPanes().size() > 0){
          _addItem.setEnabled(true);
          _addSystem.setEnabled(true);
          _saveSelection.setEnabled(true);
          _updateMarketData.setEnabled(true);
        }
        else {
          _addItem.setEnabled(false);
          _addSystem.setEnabled(false);
          _saveSelection.setEnabled(false);
          _updateMarketData.setEnabled(false);
        }
      }

      @Override
      public void menuDeselected(MenuEvent arg0) {
      }

      @Override
      public void menuCanceled(MenuEvent arg0) {
      }
    });
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == _addCollection) {
      String collectionName = (String) JOptionPane.showInputDialog(null, "Name your collection", "new collection", 1);
      if (collectionName != null) {
        _collectionPane.addCollection(collectionName);
      }
    }
    else if (event.getSource() == _addItem) {
      String searchString = (String) JOptionPane.showInputDialog("enter item name or id");
      if (searchString != null) {
        searchString = searchString.toLowerCase();
        String query = "SELECT * FROM items WHERE LOWER (eve_item_name) LIKE '%" + searchString + "%';";
        System.out.println(query);
        _createResultDialog(EVEData.Items, query);
      }
    }
    else if (event.getSource() == _addSystem) {
      String searchString = (String) JOptionPane.showInputDialog("enter system name or id");
      if (searchString != null) {
        searchString = searchString.toLowerCase();
        String query = "SELECT * FROM systems WHERE LOWER (eve_system_name) LIKE '%" + searchString + "%';";
        _createResultDialog(EVEData.Systems, query);
      }
    }
    else if (event.getSource() == _saveSelection) {
      _dataBase._storeCurrentUser(_collectionPane);
    }
    else if (event.getSource() == _updateMarketData) {
      getParent().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      for (EVEItem item : _collectionPane.getSelectedCollection().getItems()) {
        try {
          _evecctrl._retrieveMarketData(item);
        }
        catch (IOException | SAXException e) {
          e.printStackTrace();
        }
      }
      getParent().setCursor(Cursor.getDefaultCursor());
    }
  }

  private void _createResultDialog(EVEData data, String query) {
    try {
      _dataBase._connect();
      ArrayList<String[]> results = new ArrayList<String[]>();
      ResultSet items = _dataBase._executeQuery(query);

      while (items.next()) {
        String[] row = { items.getString(1), items.getString(2), items.getString(3) };
        System.out.println(row);
        results.add(row);
      }
      new ResultDialog(data, results, _collectionPane, _evecctrl);
      items.close();
      _dataBase._disconnect();
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }
}
